package com.bkav.portal.potlet;

import java.io.IOException;


import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;

import com.bkav.portal.potlet.model.Employee;
import com.bkav.portal.potlet.model.impl.EmployeeImpl;
import com.bkav.portal.potlet.service.EmployeeLocalServiceUtil;
import com.liferay.counter.service.CounterLocalServiceUtil;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;

/**
 * Portlet implementation class EmployeePortlet
 */
public class EmployeePortlet extends MVCPortlet {

	public void addEmployee(ActionRequest actionRequest,
			ActionResponse actionResponse) throws PortletException, IOException {
		String employeeName = ParamUtil.getString(actionRequest, "employeeName");
		String employeeDesc = ParamUtil.getString(actionRequest, "employeeDesc");
		String employeeJob = ParamUtil.getString(actionRequest, "employeeJob");
		Employee employee = new EmployeeImpl();
		// set primary key
		long empId = 01;

		try {
			empId = CounterLocalServiceUtil
					.increment(this.getClass().getName());
		} catch (SystemException e) {
			e.printStackTrace();
		}
		
		employee.setEmployeeId(empId);

		// set UI fields
		employee.setEmployeeName(employeeName);
		employee.setEmployeeDesc(employeeDesc);
		employee.setEmployeeJob(employeeJob);
		// set audit field(s)

		try {
			EmployeeLocalServiceUtil.addEmployee(employee);
		} catch (SystemException e) {
			e.printStackTrace();
		}

	}

	public void updateEmployee(ActionRequest actionRequest,
			ActionResponse actionResponse) {
		// Get value for object category
		String employeeName = ParamUtil.getString(actionRequest, "employeeName");
		String employeeDesc = ParamUtil.getString(actionRequest, "employeeDesc");
		String employeeJob = ParamUtil.getString(actionRequest, "employeeJob");
		long employeeId = ParamUtil.getLong(actionRequest, "employeeId");
		try {
			Employee employee = EmployeeLocalServiceUtil
					.getEmployee(employeeId);
			employee.setEmployeeName(employeeName);
			employee.setEmployeeDesc(employeeDesc);
			employee.setEmployeeJob(employeeJob);
			EmployeeLocalServiceUtil.updateEmployee(employee);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public void removeEmployee(ActionRequest actionRequest,
			ActionResponse actionResponse) {
		long employeeId = ParamUtil.getLong(actionRequest, "employeeId");
		try {
			EmployeeLocalServiceUtil.deleteEmployee(employeeId);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
	
	//------------------------------------------------------------
	
	

}
